package com.yto.hrreport.event.buttons {
	
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.Tree;
	
	public class EditButtonEvent extends ButtonEvent {
		
		private static var editEvent : EditButtonEvent = new EditButtonEvent();
		private static var parentName : String;
		
		public function EditButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : EditButtonEvent {
			EditButtonEvent.parentName = parentName;
			return editEvent;
		}
		
		public function EditEvent(success : Boolean ) : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			var headCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			var sysTree : Tree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));
			var sysPanel : Panel = Panel(hdivBox.getChildByName(parentName + "_sysFunc"));
			
			if(sysPanel == null) {
//				sysPanel  = Panel(hdivBox.getChildByName(parentName + "_sysTemplate"));
			} else {
				var sysCanvas : Canvas = Canvas(sysPanel.getElementAt(0));
				
				var txtNodeCode : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeCode"));
				var txtNodeId : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeId"));
				var txtNodeName : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeName"));
				var txtParentNodeCode : TextInput = TextInput(sysCanvas.getChildByName(parentName + "_txtParentNodeCode"));
				var chkIsParent : CheckBox = CheckBox(sysCanvas.getChildByName(parentName + "_chkIsParent"));
				
				if(sysTree.selectedItem != null) {
					var item : Object = sysTree.selectedItem;
					
					if(success) {
						
						item.state = "edit";
						
						sysTree.enabled = false;
						
						if(item.children != null && item.children != "[]") {
							txtNodeCode.enabled = false;
							chkIsParent.enabled = false;
						} else {
//							txtNodeCode.enabled = true;
							chkIsParent.enabled = true;	
						}
						
						txtNodeId.enabled = true;
						txtNodeName.enabled = true;
						
					} else {
						
						if(item != null) {
							
							item.state = null;
							
							txtNodeCode.text = item.nodecode;
							txtNodeId.text = item.nodeid;
							txtNodeName.text = item.nodename;
							txtParentNodeCode.text = item.parentcode;
							chkIsParent.selected = item.isparent == "Y" ? true : false;
						}
						
						sysTree.enabled = true;
						txtNodeCode.enabled = false;
						txtNodeId.enabled = false;
						txtNodeName.enabled = false;
						chkIsParent.enabled = false;
						txtParentNodeCode.enabled = false;
						
					}
					
					updateButtons(null , "sysEdit" , headCanvas , success); 
					
					if(sysTree.selectedItem.isparent == "N")
						Button(headCanvas.getChildByName(parentName + "_btnAdd")).enabled = false;
					
				}
				
				
			}
			
		}
		
	}
}